package com.xuecheng.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.system.model.dto.CourseCategoryDTO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.system.constant.SystemErrorCode;
import com.xuecheng.system.convert.CourseCategoryConvert;
import com.xuecheng.system.entity.CourseCategory;
import com.xuecheng.system.entity.ex.CourseCategoryNode;
import com.xuecheng.system.mapper.CourseCategoryMapper;
import com.xuecheng.system.service.CourseCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {

    /**
     * <p>
     * 查询课程分类 按树形结构封装数据
     * </p>
     *
     * @return List<CourseCategoryDTO>
     */
    @Override
    public List<CourseCategoryDTO> queryTreeNodes() {

        CourseCategoryMapper baseMapper = this.getBaseMapper();
        List<CourseCategoryNode> treeNode = baseMapper.selectTreeNode();

        List<CourseCategoryDTO> categoryDTOList = Collections.emptyList();

        if (CollectionUtils.isNotEmpty(treeNode)) {
            categoryDTOList = CourseCategoryConvert.INSTANCE.courseCategoryNodeToDtoList(treeNode);
        }
        return categoryDTOList;
    }

    /**
     * <p>
     * 根据id查询课程分类信息
     * </p>
     *
     * @param id 分类id
     * @return RestResponse<CourseCategoryDTO>
     */
    @Override
    public RestResponse<CourseCategoryDTO> getCategoryById(String id) {

        CourseCategory category = this.getById(id);
        if (ObjectUtils.isEmpty(category)) {
            return RestResponse.validfail(SystemErrorCode.E_110000);
        }
        CourseCategoryDTO categoryDTO = CourseCategoryConvert.INSTANCE.CourseCategoryToDTO(category);

        return RestResponse.success(categoryDTO);
    }
}
